
import ala from '@/service/ala'
import Vue from 'vue'


/*
* 登录
*/
export async function userLogin (model, befterTo) {
  if (model.username.length < 3) {
    Vue.prototype.$message.warning('账号最短为 3 个字符')
    return
  }
  if (model.password.length < 6) {
    Vue.prototype.$message.warning('密码最短为六位数')
  }
  var response = await ala.httpPost('api/member/login', model)
  if (response) {
    ala.userStorage(response)
    ala.localRemove('wechat_logincount')
    ala.success('恭喜您,登录成功')
    await this.userLoginAfterTo(befterTo)
  }
}



/*
* 跳转到登录页面
*/
export function userToLoginPage () {
  ala.erroe('请先登录')
  ala.localSet('browse_historys', window.location.href)
  ala.push('/login')
}


/*
* 登录后跳转
*/
export async function userLoginAfterTo (befterTo) {
  if (befterTo !== undefined) {
    ala.push(befterTo)
    return
  }
  if (ala.userIsLogin()) {
    // 跳转到上一级页面
    var previousPage = ala.localGet('browse_historys')
    if (ala.strIsEmpty(previousPage)) {
      var themeRoute = await ala.themeRoute()
      ala.push(themeRoute.index)
    } else {
      ala.localRemove('browse_historys')
      ala.push(previousPage)
    }
  }
}